package com.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flower.domain.ApiResult;
import com.flower.domain.entity.BackageUser;
import com.flower.domain.user.UserQuery;
import com.flower.mapper.BackageUserMapper;
import com.flower.service.BackageUserService;
import com.flower.utils.SecurityUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * (BackageUser)表服务实现类
 *
 * @author lixinghe
 * @since 2022-10-11 20:53:56
 */
@Service("backageUserService")
public class BackageUserServiceImpl extends ServiceImpl<BackageUserMapper, BackageUser> implements BackageUserService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public ApiResult<BackageUser> BackAgeUserInfo() {
        Long userId = SecurityUtils.getUserId();
        BackageUser backageUser = getById(userId);
        log.warn(backageUser.toString());
        return ApiResult.ok(backageUser);
    }

    @Override
    public ApiResult<Boolean> BackAgeUserInfoAdd(BackageUser backageUser) {
        BackageUser user = new BackageUser();
        user.setName(backageUser.getName());
        user.setPassword(passwordEncoder.encode(backageUser.getPassword()));
        user.setNickName(backageUser.getNickName());
        user.setAuthority(backageUser.getAuthority());
        user.setAccountStatus(1);
        user.setAvater("\thttp://rio71gsrn.bkt.gdipper.com/flower/image/%E5%B0%8F%E5%85%94.png\n");

        save(user);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Map<String,Object>> BackAgeUserList(int page, int size, UserQuery userQuery) {
        //构建分页查询
        Page<BackageUser> userPage = new Page<>(page,size);
        //如果前端传来了值 构建条件查询器
        LambdaQueryWrapper<BackageUser> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isBlank(userQuery.getUserName()) || !Objects.isNull(userQuery.getCreateTime())){
            wrapper.eq(!StringUtils.isBlank(userQuery.getUserName()),BackageUser::getName,userQuery.getUserName());
            wrapper.like(!StringUtils.isBlank(userQuery.getCreateTime()),BackageUser::getCreateTime,userQuery.getCreateTime() + "%");
        }
        //按创建时间升序排列
        wrapper.orderByAsc(BackageUser::getCreateTime);
        Page<BackageUser> page1 = page(userPage,wrapper);
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",page1.getRecords());
        map.put("total",page1.getTotal());
        return ApiResult.ok(map);
    }

    @Override
    public ApiResult<Boolean> upAccountStatus(int id,int accountStatus) {
        if(Objects.isNull(id)){
            throw new RuntimeException("参数非法");
        }
        LambdaUpdateWrapper<BackageUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BackageUser::getId,id);
        wrapper.set(accountStatus == 1 ,BackageUser::getAccountStatus,0);
        wrapper.set(accountStatus == 0 ,BackageUser::getAccountStatus,1);
        update(wrapper);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Boolean> deleteBackAgeUser(long id) {
        //正常项目应该是逻辑删除 这里为了省内存 直接物理删除了
        if(Objects.isNull(id)){
            throw new RuntimeException("参数非法");
        }
        removeById(id);
        return ApiResult.ok();
    }
}
